package algorithm.fft;

public class FFTNodeFactory implements IFFTNodeFactory {
	private  FFTNode[] stack;
	private  int top;
	private  FFTNode topNode; 
	private Trigonometry trigonometry; 
	private WorkMemory wm; 
	public FFTNodeFactory(int size,int dim,Trigonometry t, WorkMemory wm){
		stack=new FFTNode[size]; 
		this.wm=wm; 
		for (int i=0; i<size; i++){
			stack[i]=new FFTNode(this,wm); 
		}
		top=size; 
		trigonometry=t; 
	}
	/* (non-Javadoc)
	 * @see algorithm.fft.IFFTNodeFactory#createRootNode(int, int, int)
	 */
	public FFTNode createRootNode(int from, int to,int order){
		return createFFTNode(from,1,to,to,FFTNode.Node_ROOT,order,null); 
	}
	public  void create2FFTNodes(FFTNode[] nodes){
		nodes[0]=stack[top-1]; 
		nodes[1]=stack[top-2]; 
		top-=2; 
	}
	/* (non-Javadoc)
	 * @see algorithm.fft.IFFTNodeFactory#createFFTNode(int, int, int, int, short, int, algorithm.fft.FFTNode)
	 */
	public FFTNode createFFTNode(){
		topNode=stack[top-1];
		top--;
		return topNode; 
	}
	public FFTNode createFFTNode(int from, int step, int to,int size,short type,int order,FFTNode parent){
		topNode=stack[top-1];
		FFTNode.setInfo(topNode,false,from, step, to, size, type,order, parent);	
		top--;
		return topNode;
		
		/*
		topNode=new FFTNode(this,wm); 
		topNode.setInfo(from, step, to, size, type,order, parent);
		return topNode;
		*/ 
	}
	/* (non-Javadoc)
	 * @see algorithm.fft.IFFTNodeFactory#freeNode(algorithm.fft.FFTNode)
	 */
	public void freeNode(FFTNode node){
		stack[top++]=node; 
	}
	public boolean isAllFree(){
		return top==stack.length;  
	}
	/* (non-Javadoc)
	 * @see algorithm.fft.IFFTNodeFactory#getTrigonometry()
	 */
	public Trigonometry getTrigonometry() {
		return trigonometry;
	}
}
